package com.ybkj.daijia.server.event.listener.freight;

import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.PushData;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.event.model.freight.FreightOrderArriveEvent;
import com.ybkj.daijia.server.freight.Order;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsService;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class FreightOrderArriveEventListener implements
    ApplicationListener<FreightOrderArriveEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(FreightOrderArriveEventListener.class);


    @Autowired
    private MemberService memberService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private SmsService smsService;

    @Autowired
    private CompanyService companyService;

    @Override
    @Async
    public void onApplicationEvent(FreightOrderArriveEvent event) {

        Order order = (Order) event.getSource();
        Passenger passenger = event.getPassenger();
        passenger.setCid(memberService.findCid(passenger.getId()));
        if (StringUtils.isNotBlank(passenger.getCid())) {
            try {
                //发送设备推送
                String address = "";
                if (order != null) {
                    address = order.getStartAddress();
                }
                PushData pushData = new PushData();
                pushData.setType("WAIT_ORDER_FREIGHT");
                pushData.setTitle("服务人员到达预约地");
                pushData.setDescription("服务人员已到达预约地【" + address + "】");
                pushData.setData(order.getId());
                GeTuiMember.push(passenger, pushData, settingUtils.get());
            } catch (IOException e) {
                e.getMessage();
            }

        }

        //发送短信
        Company orderCompany = companyService.findOne(passenger.getCompanyId());
        Company finalCompany = orderCompany;
        Company main = companyService.getMain();
        Setting setting = settingUtils.get();
        if (!main.getId().equals(orderCompany.getId()) && setting.isSmsMainCompany()) {
            finalCompany = main;
        }
        SmsTemplet templet = smsService.findByAlias(finalCompany.getId(), SmsType.driverAccept);
        if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
            try {
                //smsService.sendSms(order.getPassengerPhone(), "arriveFromFreightPlace", "{\"服务人员名\":\""+order.getEmployName()+"\",\"服务人员工号\":\""+order.getEmployNo()+"\",\"服务人员电话\":\""+ order.getEmployPhone()+"\",\"预约地\":\""+order.getStartAddress()+"\",\"订单号\":\""+"***"+order.getOrderNo().substring(8)+"\"}", finalCompany.getId(), 0, finalCompany.getSignName());
                //短信发送（改）
                JSONObject smsParam = new JSONObject();
                smsParam.put("name", order.getEmployName());
                smsParam.put("user", order.getEmployNo());
                smsParam.put("tel", order.getEmployPhone());
                smsParam.put("order", order.getOrderNo().substring(8));
                smsService.sendSms(order.getPassengerPhone(), templet.getTemplateId(),
                    smsParam.toJSONString(), finalCompany.getId(), 0, finalCompany.getSignName());
            } catch (Exception e) {
                logger.error("发送短信发生异常：{}", e.getMessage());
            }
        }

    }

}
